$(function(){
	search();
	
	/**
	 * 添加事件
	 */
	$('#datagrid-toolbar #add_link').linkbutton({
		onClick:function(){
			add();
		}
	});
	
	/**
	 * 更新事件
	 */
	$('#datagrid-toolbar #edit_link').linkbutton({
		onClick:function(){
			edit();
		}
	});
	
	/**
	 * 删除事件
	 */
	$('#datagrid-toolbar #cancel_link').linkbutton({
		onClick:function(){
			del();
		}
	});
});

/**
 * 查询
 */
function search(){
	$.ajax({
		type : "GET",
		url : ctx + "/tree/menu",
		dataType : "json",
		success : function(data) {
			if(!showMsg(data)){
				return;
			}
			
			var setting = {
					check: {
						enable: false,
					},
					data: {
						simpleData: {
							enable: true,
							pIdKey: "pid",
						},
						key:{
							name:'name'
						}
					},
					callback: {
						onClick:getResource
					}
				};
			$.fn.zTree.init($("#menuTree"), setting, data.content);
			var tree = $.fn.zTree.getZTreeObj("menuTree");
			var node = tree.getNodeByParam("id", "1");
			tree.selectNode(node);
			if(tree.setting.callback.onClick != null){
				tree.setting.callback.onClick(null, tree.setting.treeId, node);//调用事件
			}
			
			$("#resource").tabs({
				border:false,
			    onSelect:function(title,index){
			    	var treeObj = $.fn.zTree.getZTreeObj("menuTree");
		    		var nodes = treeObj.getSelectedNodes();
		    		if(nodes.length == 0)
		    			return;
		    		getResource(null,null,nodes[0]);
			    }
			});
		}
	});
}
function getResource(event,treeId,treeNode){
	var resource_tab = $('#resource').tabs('getSelected');  // get selected panel
	if(resource_tab.panel('options').title == '资源管理')
		getMenu(event,treeId,treeNode);
	else if(resource_tab.panel('options').title == '行为管理'){
		getAction(event,treeId,treeNode);
	}
}
function getMenu(event,treeId,treeNode){
	$.ajax({
		type : "GET",
		url : ctx + "/menu/query",
		data :{id:treeNode.id},
		dataType : "json",
		success : function(data) {
			if(!showMsg(data)){
				return;
			}
			
			$("#datagrid").datagrid({
				border : false,
				fitColumns : true,
				checkbox : true,
				fit : true,
				remoteSort : false,
				rownumbers : true,
				singleSelect : true,
				toolbar : "#datagrid-toolbar",
				data : data.content
			});
		}
	});
}

/**
 * 添加
 */
function add(){
	var menuTree = $.fn.zTree.getZTreeObj("menuTree");
	var nodes = menuTree.getSelectedNodes();
	if(nodes.length == 0){
		message("请选中父节点");
		return;
	}
	if(nodes[0].level == '3'){
		message("该节点无法添加子节点");
		return;
	}
	$('#dialog').dialog({
	    title: '添加',
	    width: 550,
	    height: 500,
	    closed: false,
	    cache: false,
	    modal: true,
	    href:ctx + "/web/system/menu/add.jsp",
	    onClose:function(){
	    	destroy('dialog','#dialogDiv');
	    },
	    onLoad:function(){
	    	$("#addform").form('load',{});
	    	findDrawerByParentCode(资源类型, setDrawerTypeValue);
	    	findDrawerByParentCode(系统类型, setSystemTypeValue);
	    },
	    buttons: [{
            text:'保存',
            iconCls:'icon-ok',
            handler:function(){
            	
	    		var bool = $("#addform").form('enableValidation').form('validate');
            	if(!bool){
            		return;
            	}
            	var data = $("#addform").serializeJson();
            	/*var name = $("#name").textbox("getValue");
            	var status = $("#status").combobox("getValue");
            	var sort = $("#sort").textbox("getValue");
            	var path = "";
            	if($("#path").length != 0){
            		path = $("#path").textbox("getValue");
            	}
            	
            	var data = {};
            	data.name = name;
            	data.path = path;
            	data.status = status;
            	data.sort = sort;*/
            	data.pid = nodes[0].id;
            	$.ajax({
            		type : "POST",
            		url : ctx + "/menu/create",
            		data :data,
            		dataType : "json",
            		success : function(data) {
            			if(!showMsg(data,"添加成功")){
            				return;
            			}
            			var menuTree = $.fn.zTree.getZTreeObj("menuTree");
            			menuTree.addNodes(nodes[0], data.content);
            			$('#datagrid').datagrid('appendRow',data.content);
            			$('#dialog').dialog('close');
            		}
            	});
            }
        },{
            text:'取消',
            iconCls:'icon-cancel',
            handler:function(){
            	$('#dialog').dialog('close');
            }
        }]
	});
}

/**
 * 更新
 */
function edit(){
	var row = undefined;
	row = getSelectedRow("#datagrid");
	if(!row){
		return;
	}
	$('#dialog').dialog({
	    title: '更新',
	    width: 600,
	    height: 550,
	    closed: false,
	    cache: false,
	    modal: true,
	    href:ctx + "/web/system/menu/add.jsp",
	    onClose:function(){
	    	destroy('dialog','#dialogDiv');
	    },
	    onLoad:function(){
	    	var menuTree = $.fn.zTree.getZTreeObj("menuTree");
	    	var nodes = menuTree.getSelectedNodes();
	    	/*if(nodes[0].level == 2){
	    		$("#menutable").append("" +
	    		"<tr>" + 
	            "<td><span style='font-size: 15px;'>路径:</span></td>" +
	            "<td><input id='url' type='text' style='width:300px' name='url' class='easyui-textbox' data-options=\"required:true,missingMessage:\'名称必须填写\'\"></input></td>"+
	    		"</tr>");
	    		$("#url").textbox();
	    		$("#url").textbox("setValue",row.url);
        	}*/
	    	$("#addform").form('load',row);
	    	findDrawerByParentCode(资源类型, setDrawerTypeValue);
	    	findDrawerByParentCode(系统类型, setSystemTypeValue);
	    },
	    buttons: [{
            text:'保存',
            iconCls:'icon-ok',
            handler:function(){
            	var bool = $("#addform").form('enableValidation').form('validate');
            	if(!bool){
            		return;
            	}
            	/*var name = $("#name").textbox("getValue");
            	var status = $("#status").combobox("getValue");
            	var sort = $("#sort").textbox("getValue");
            	var url = "";
            	if($("#url").length != 0){
            		url = $("#url").textbox("getValue");
            	}
            	var data = {};
            	data.id = row.id;
            	data.name = name;
            	data.url = url;
            	data.status = status;
            	data.sort = sort;*/
            	var data = $("#addform").serializeJson();
            	data.id = row.id;
            	$.ajax({
            		type : "POST",
            		url : ctx + "/menu/update",
            		data :data,
            		dataType : "json",
            		success : function(data) {
            			if(!showMsg(data,"修改成功")){
            				return;
            			}
            			
        				var menuTree = $.fn.zTree.getZTreeObj("menuTree");
            			var node = menuTree.getNodeByParam("id", row.id, null);
            			node.name = data.content.name;
            			menuTree.updateNode(node);
            			
            			var index = $('#datagrid').datagrid('getRowIndex',row);
            			
            			$('#datagrid').datagrid('updateRow',{
            				index: index,
        					row: data.content
        				});
            			
            			$('#dialog').dialog('close');
            		}
            	});
            }
        },{
            text:'取消',
            iconCls:'icon-cancel',
            handler:function(){
            	$('#dialog').dialog('close');
            }
        }]
	});
}

/**
 * 删除
 */
function del(){
	var row = null;
	row = getSelectedRow("#datagrid");
	if(!row){
		return;
	}
	
	$.messager.confirm('提示', '确定要删除吗?', function(bool){
	    if (bool){
	    	$.ajax({
	    		type : "POST",
	    		url : ctx + "/menu/delete",
	    		data :{id : row.id},
	    		dataType : "json",
	    		success : function(data) {
	    			if(!showMsg(data,"删除成功")){
        				return;
        			}
	    			
	    			var menuTree = $.fn.zTree.getZTreeObj("menuTree");
        			var node = menuTree.getNodeByParam("id", row.id, null);
        			menuTree.removeNode(node);
    				
    				var index = $('#datagrid').datagrid('getRowIndex',row);
	    			$("#datagrid").datagrid('deleteRow',index);
	    		}
	    	});
	    }
	});
}

/**
 * 设置显示区域下拉框
 * @param data
 */
function setDrawerTypeValue(data){
	var value = $('#addform #displayAreaCode').combobox('getValue');
	$('#addform #displayAreaCode').combobox({ 
		valueField:'code',
        textField:'name',
        data:data,
        panelHeight:'auto'
	});
	
	if($.trim(value).length != 0){
		$('#addform #displayAreaCode').combobox('select',value);
	}
}

/**
 * 设置系统类型下拉框
 * @param data
 */
function setSystemTypeValue(data){
	var value = $('#addform #systemTypeCode').combobox('getValue');
	$('#addform #systemTypeCode').combobox({ 
		valueField:'code',
        textField:'name',
        data:data,
        panelHeight:'auto'
	});
	
	if($.trim(value).length != 0){
		$('#addform #systemTypeCode').combobox('select',value);
	}
}